package bancodedados;

import java.util.ArrayList;
import java.util.List;

import estruturas.Aluno;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


	@SuppressLint("DefaultLocale")
	public class CadastroAlunoDAO{
		private SQLiteDatabase db;
		private Helper helper;
		private String[] columns = {"matricula", "nome","endereco","telefone","sexo","turma"};
		private long insertMatricula;
		
		public CadastroAlunoDAO(Context context)
		{
			helper = new Helper(context);
		}
		
		public void open() throws SQLException
		{
			db = helper.getWritableDatabase();
		}
		
		public void close()
		{
			db.close();
		}

		public Aluno addCadastroAluno(String n)
		{
			ContentValues values = new ContentValues();
			values.put("nome", n);
			setInsertMatricula(db.insert("cadastroAluno", null, values));
			Cursor cursor = db.query("CadastroAluno", columns, null, null, null, null, null,null);
			
			cursor.moveToFirst();
			Aluno newCadastroAluno = cursorToCadastroAluno(cursor);
			cursor.close();
			
			return newCadastroAluno;
		}
		
		public List<String> getLista()
		{
			List<String> cadastroAluno = new ArrayList<String>();
			
			Cursor cursor = db.query("CadastroAluno", columns, null, null, null, null, null,null);
			
			cursor.moveToFirst();
			while(!cursor.isAfterLast())
			{
				cadastroAluno.add(cursorToCadastroAluno(cursor).getNome());
				cursor.moveToNext();
			}
			
			cursor.close();
			return cadastroAluno;
		}
		
		public boolean existeCadastroAluno(String n){
			Cursor cursor = db.query("CadastroAluno", columns, "Lower(nome) = '" + n.toLowerCase() + "'", null, null, null, null,null);
			
			if (cursor.getCount()>0){
				cursor.close();
				return true;
			}else{
				cursor.close();
				return false;
			}
			
		}
		
		
		private Aluno cursorToCadastroAluno(Cursor cursor)
		{
			Aluno cadastro = new Aluno();
			
			cadastro.setMatricula(cursor.getInt(0));
			cadastro.setNome(cursor.getString(1));
			cadastro.setEndereco(cursor.getString(1));
			cadastro.setTelefone(cursor.getString(1));
			cadastro.setSexo(cursor.getString(1));
			cadastro.setTurma(cursor.getString(1));
			
			
			return cadastro ;
		}

		public long getInsertMatricula() {
			return insertMatricula;
		}

		public void setInsertMatricula(long insertMatricula) {
			this.insertMatricula = insertMatricula;
		}
	}


